# !/usr/bin/env python
# -*- coding: utf-8 -*-
# @File  : 单一聚合push操作.py
# @Author: dongguangwen
# @Date  : 2025-06-14 20:30
from pymongo import MongoClient

# 连接到MongoDB（默认端口27017）
client = MongoClient('mongodb://root:root123@192.168.1.119:27017/')

# 创建数据库和集合
db = client['learning_mongodb']
collection = db['users']


pipeline = [
    {
        "$group": {
            "_id": None,
            "all_tags": {"$push": "$tags"}
        }
    }
]
result = list(db.users.aggregate(pipeline))
print(result)
print("所有用户的标签列表:", result[0]["all_tags"])  # 注意：会把所有的全部列出来


"""
[{'_id': None, 'all_tags': [['music'], ['sports', 'reading', 'travel'], ['travel', 'sports', 'coding'], ['music', 'coding'], ['reading', 'music'], ['sports', 'reading'], ['sports'], ['reading', 'travel'], ['coding'], ['coding', 'travel', 'sports']]}]
所有用户的标签列表: [['music'], ['sports', 'reading', 'travel'], ['travel', 'sports', 'coding'], ['music', 'coding'], ['reading', 'music'], ['sports', 'reading'], ['sports'], ['reading', 'travel'], ['coding'], ['coding', 'travel', 'sports']]
"""
